﻿using System;
using System.Collections.Generic;
using System.Linq;
using LBoL.Base;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Battle.Interactions;
using LBoL.Core.Cards;
using LBoL.Core.StatusEffects;
using LBoL.EntityLib.StatusEffects.Koishi;
using LBoL.EntityLib.StatusEffects.Neutral.Blue;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;
using ProbeMod.StatusEffects;

namespace ProbeMod.Cards
{
    public sealed class ProbeNaiDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            CardConfig config = GetCardDefaultConfig();

            config.Rarity = Rarity.Rare;
            config.Type = CardType.Skill;
            config.TargetType = TargetType.Nobody;

            config.Colors = new List<ManaColor>() { ManaColor.Colorless };
            config.Cost = new ManaGroup() { Colorless = 2,Any = 3 };
            config.UpgradedCost = new ManaGroup() { Colorless = 1, Any = 2 };


            config.Index = CardIndexGenerator.GetUniqueIndex(config);
            return config;
        }
    }

    /// <summary>
    /// 奶
    /// </summary>
    [EntityLogic(typeof(ProbeNaiDef))]
    public sealed class ProbeNai : ProbeCard
    {
        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            var units = Battle.AllAliveUnits.ToList();
            foreach (var unit in units)
            {
                var effects = unit.StatusEffects.ToList();
                foreach (var effect in effects)
                {             
                    if(TryAddReversalStatusEffect(effect, out var status))
                    {
                        var level = effect.HasLevel ? effect.Level : 0;
                        var duration = effect.HasDuration ? effect.Duration : 0;
                        var limit = effect.Limit;
                        var count = effect.HasCount ? effect.Count : 0;
                        yield return new RemoveStatusEffectAction(effect);
                        yield return new ApplyStatusEffectAction(status, unit, level, duration, limit: limit, count: count, occupationTime: 0.2f)
                        {
                            Args =
                            {
                                Effect =
                                {
                                    SourceCard = this
                                }
                            }
                        };

                    }
                }
            }
            yield return null;

        }

        private bool TryAddReversalStatusEffect(StatusEffect effect,out Type status)
        {
            // 暂得/失火力
            if (effect is TempFirepower)
            {
                status = typeof(TempFirepowerNegative);
                
                return true;
                
            }
            if (effect is TempFirepowerNegative)
            {
                status = typeof(TempFirepower);
                return true;
            }
            // 失去/ 火力
            if (effect is Firepower)
            {
                status = typeof(FirepowerNegative);
                return true;
            }
            if (effect is FirepowerNegative)
            {
                status = typeof(Firepower);
                return true;
            }

            // 暂得/失灵力
            if (effect is TempSpirit)
            {
                status = typeof(TempSpiritNegative);
                return true;
            }
            if (effect is TempSpiritNegative)
            {
                status = typeof(TempSpirit);
                return true;
            }

            //  灵力
            if (effect is Spirit)
            {
                status = typeof(SpiritNegative);
                return true;
            }
            if (effect is SpiritNegative)
            {
                status = typeof(Spirit);
                return true;
            }

            // 锁定/闪避
            if (effect is LockedOn)
            {
                status = typeof(Graze);
                return true;
            }
            if (effect is Graze)
            {
                status = typeof(LockedOn);
                return true;
            }

            // 热情/冷静
            if (effect is MoodPeace)
            {
                status = typeof(MoodPassion);
                return true;
            }
            if (effect is MoodPassion)
            {
                status = typeof(MoodPeace);
                return true;
            }

            //  脆弱/硬强
            if (effect is ProbeReversalFragilSe)
            {
                status = typeof(Fragil);
                return true;
            }
            if (effect is Fragil)
            {
                status = typeof(ProbeReversalFragilSe);
                return true;
            }

            // 虚弱/实强
            if (effect is ProbeReversalWeakSe)
            {
                status = typeof(Weak);
                return true;
            }
            if (effect is Weak)
            {
                status = typeof(ProbeReversalWeakSe);
                return true;
            }
            
            if(IsUpgraded)
            {
                if (effect is ProbeReversalLimitedDamageSe)
                {
                    status = typeof(LimitedDamage);
                    return true;
                }
                if (effect is LimitedDamage)
                {
                    status = typeof(ProbeReversalLimitedDamageSe);
                    return true;
                }
            }
            status = null;
            return false;
        }

    }
}
